package dtaskboard.server.dao;

import java.util.List;

import javax.persistence.Query;

import dtaskboard.entities.Project;

public class ProjectDao extends GenericDao<Project> {

  private static final String BY_NAME = "FROM " + Project.class.getSimpleName()
      + " WHERE name = :name";

  @Override
  public Class<Project> getEntityClass() {
    return Project.class;
  }

  public Project findByName(String projectName) {
    Query query = EMFactory.get().createQuery(ProjectDao.BY_NAME);
    query.setParameter("name", projectName);

    @SuppressWarnings("unchecked")
    List<Project> results = query.getResultList();
    if (results == null || results.size() != 1) {
      return null;
    }
    return results.get(0);
  }

}
